package parseAndHandleTool;

import com.oocl.crm_system.Customer;
import com.oocl.crm_system.DoublyList;
import com.oocl.crm_system.Node;

public class ParseCmdForUpdate extends ParseCmd{
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 * @return void
	 * @Description update customer's message by id
	 */
	public void parseCmdForUpdate(DoublyList<Customer> list,String inputByConsole){
		String input = inputByConsole;
		
		String[] str = input.split(" ");
		int id = Integer.parseInt(str[1]);         //get the customer's id
		System.out.println("customer's id is ====================" + id);
		input = str[2];

		//handle filed which you want to update
		String[] buf = input.split(",");
		String[] buf1 = new String[2];
		String[] buf2 = new String[buf.length];
		String[] buf3 = new String[buf.length];
		
		for(int i = 0; i < buf.length; i++){
			buf1 = buf[i].split(":");
			buf2[i] = buf1[1];
			buf3[i] = buf1[0];
			System.out.println(buf2[i] + ":" + buf3[i]);
		}
		
		for(int i = 0; i < list.nodeNum; i++){
			Node theNode = list.findNodeByIndex(i);
			Customer thecustomer = (Customer) theNode.value;
			
			if(thecustomer.getId() == id){
				
				list.delNode(thecustomer);			//delete the customer first
				
				for(int j = 0; j < buf.length; j++){
					if(buf3[j].equals("name")){
						thecustomer.setName(buf2[j]);
					}
					else if(buf3[j].equals("sex")){
						thecustomer.setSex(buf2[j]);
					}
					else if(buf3[j].equals("birthday")){
						thecustomer.setBirthday(buf2[j]);
					}
					else if(buf3[j].equals("address")){
						thecustomer.setAddress(buf2[j]);
					}
					else if(buf3[j].equals("call")){
						thecustomer.setCall(buf2[j]);
					}
					else if(buf3[j].equals("id")){
						thecustomer.setId(Integer.parseInt(buf2[j]));
					}
					else{
						
					}
				}
				
				list.insertTail(thecustomer);			//add the customer back to doublyList second
			}
		}		
	}
}
